package com.gyf.algorithm.struc.list;

public abstract class AbsList<E> implements List<E> {
    protected int size;

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public boolean contain(E e) {
        return indexOf(e) != ELEMENT_NOT_FOUND;
    }

    @Override
    public void add(E e) {
        add(size, e);
    }

    protected void outOfBounds(int index) {
        if (index < 0 || index > size) {
            throw new IndexOutOfBoundsException(String.format("index must between 0 and %d", size));
        }
    }

    protected void rangeCheck(int index) {
        if (index < 0 || index > size) {
            outOfBounds(index);
        }
    }

    protected void rangeCheckForAdd(int index) {
        if (index < 0 || index >= size) {
            outOfBounds(index);
        }
    }

}
